package org.example.study11.condition;
// 场景2：多条件精确控制（读写分离）
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 资源控制器 - 演示多Condition的精确控制
 */
public class ResourceController {
    private int resourceA = 0;              // 资源A
    private int resourceB = 0;              // 资源B
    private final ReentrantLock lock;       // 可重入锁
    private final Condition conditionA;     // 资源A可读条件
    private final Condition conditionB;     // 资源B可读条件
    private final Condition writeCondition; // 可写条件

    public ResourceController() {
        this.lock = new ReentrantLock();    // 初始化锁
        this.conditionA = lock.newCondition(); // 创建资源A的条件
        this.conditionB = lock.newCondition(); // 创建资源B的条件
        this.writeCondition = lock.newCondition(); // 创建写条件
    }

    /**
     * 写入资源A
     */
    public void writeToA(int value) throws InterruptedException {
        lock.lock();                        // 获取锁
        try {
            while (resourceA != 0) {        // 检查资源A是否已被写入
                System.out.println(Thread.currentThread().getName() + ": 资源A已有数据，等待写入");
                writeCondition.await();     // 等待可写条件
            }
            resourceA = value;              // 写入资源A
            System.out.println(Thread.currentThread().getName() + ": 写入资源A = " + value);
            conditionA.signalAll();         // 唤醒所有等待资源A的读取者
        } finally {
            lock.unlock();                  // 释放锁
        }
    }

    /**
     * 写入资源B
     */
    public void writeToB(int value) throws InterruptedException {
        lock.lock();                        // 获取锁
        try {
            while (resourceB != 0) {        // 检查资源B是否已被写入
                System.out.println(Thread.currentThread().getName() + ": 资源B已有数据，等待写入");
                writeCondition.await();     // 等待可写条件
            }
            resourceB = value;              // 写入资源B
            System.out.println(Thread.currentThread().getName() + ": 写入资源B = " + value);
            conditionB.signalAll();         // 唤醒所有等待资源B的读取者
        } finally {
            lock.unlock();                  // 释放锁
        }
    }

    /**
     * 读取资源A
     */
    public int readFromA() throws InterruptedException {
        lock.lock();                        // 获取锁
        try {
            while (resourceA == 0) {        // 检查资源A是否有数据
                System.out.println(Thread.currentThread().getName() + ": 资源A无数据，等待读取");
                conditionA.await();         // 等待资源A可读条件
            }
            int value = resourceA;          // 读取资源A的值
            resourceA = 0;                  // 清空资源A
            System.out.println(Thread.currentThread().getName() + ": 读取资源A = " + value);
            writeCondition.signal();        // 唤醒一个等待写入的线程
            return value;                   // 返回读取的值
        } finally {
            lock.unlock();                  // 释放锁
        }
    }

    /**
     * 读取资源B
     */
    public int readFromB() throws InterruptedException {
        lock.lock();                        // 获取锁
        try {
            while (resourceB == 0) {        // 检查资源B是否有数据
                System.out.println(Thread.currentThread().getName() + ": 资源B无数据，等待读取");
                conditionB.await();         // 等待资源B可读条件
            }
            int value = resourceB;          // 读取资源B的值
            resourceB = 0;                  // 清空资源B
            System.out.println(Thread.currentThread().getName() + ": 读取资源B = " + value);
            writeCondition.signal();        // 唤醒一个等待写入的线程
            return value;                   // 返回读取的值
        } finally {
            lock.unlock();                  // 释放锁
        }
    }
}
